import asyncio

from playwright.async_api import Playwright, async_playwright, expect


async def run(playwright: Playwright) -> None:
    browser = await playwright.chromium.launch(headless=False)
    context = await browser.new_context(record_har_mode="minimal", record_har_path="G:\\git\\gamecrawer\\gamecrwaer\\xdgame_har.json", service_workers="block")
    page = await context.new_page()
    await page.goto("https://www.xdgame.com/game/1693.html")
    async with page.expect_popup() as page1_info:
        await page.get_by_role("link", name="百度网盘").click()
    page1 = await page1_info.value
    await page1.get_by_role("link", name="登录会员").click()
    await page1.get_by_role("link", name="忘记密码？").click()
    await page1.get_by_placeholder("输入您的邮箱").click()
    await page1.get_by_placeholder("输入您的邮箱").fill("54xuguoxu@gmail.com")
    await page1.get_by_placeholder("输入您的邮箱").press("Tab")
    await page1.get_by_placeholder("输入验证码").fill("Q8AW")
    await page1.get_by_role("button", name="下一步").click()
    await page1.get_by_role("link", name="").click()
    await page1.goto("https://www.xdgame.com/oauth/index_do.php?action=oauth_login&type=qq&callblock=https%3A%2F%2Fwww.xdgame.com%2Fuser%2F&open=0&code=665CF8E7DAB83D0E49380A66ECD82CCF")
    await page1.goto("https://www.xdgame.com/user/")
    await page1.get_by_role("link", name=" 电脑游戏").click()
    async with page1.expect_popup() as page2_info:
        await page1.get_by_text("异星探险家/ASTRONEER/支持网络联机").click()
    page2 = await page2_info.value
    await page2.close()
    await page1.close()
    await page.close()

    # ---------------------
    await context.storage_state(path="xdgame_state.json")
    await context.close()
    await browser.close()


async def main() -> None:
    async with async_playwright() as playwright:
        await run(playwright)


asyncio.run(main())
